import com.Configure;
import com.aop.AopTest;
import com.controller.ManagerController;
import com.controller.UserController;
import com.dao.AccountDao;
import com.impl.UserDao;
import com.pojo.Account;
import com.service.AccountService;
import com.service.TransRecondService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserService;
import com.temp.LifeTest;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;

public class test {
    public static void main(String[] args) {
        ApplicationContext xml=new  ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) xml.getBean("userService");
        DataSource mydruid = (DataSource) xml.getBean("mydruid");
        System.out.println(mydruid);
        userService.talk();

        //使用注解开发
        UserController userController = (UserController) xml.getBean("userController");
        userController.talk();

        ManagerController manager = xml.getBean(ManagerController.class);
        manager.save();
    }

    @Test//注解开发的测试类
    public void testAnotation(){
        ApplicationContext app=new AnnotationConfigApplicationContext(Configure.class);
        UserService userService = app.getBean(UserService.class);
        userService.setUserDao(new UserDao());
        userService.talk();

        LifeTest lifeTest=app.getBean(LifeTest.class);
        lifeTest.talk();

//        Food bean = app.getBean(Food.class);
//        System.out.println(bean);

        DataSource bean = app.getBean(DataSource.class);
        System.out.println(bean);


    }

    @Test
    public void testlife(){
        AnnotationConfigApplicationContext app=new AnnotationConfigApplicationContext(Configure.class);
        LifeTest bean = app.getBean(LifeTest.class);
        bean.talk();
        app.close();

    }

    @Test
    public void testMybatis() throws IOException {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        InputStream inputStream= Resources.getResourceAsStream("mybatisConfigure.xml");
        SqlSessionFactory factory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = factory.openSession();
        AccountDao mapper = sqlSession.getMapper(AccountDao.class);
        Account account = mapper.findById(2);
        System.out.println(account);
    }

    @Test
    //spring整合mybatis
    public void testSpring_Mybatis(){
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(Configure.class);
        AccountService accountService = applicationContext.getBean(AccountService.class);
//        Account account = accountService.findById(1);
//        System.out.println(account);
        accountService.transMoney(1,2,666);

        //先试一试原始的转账功能
//        accountService.transMoney(1,2,100);

    }
    @Test
    public void testAop(){
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(Configure.class);
        AopTest aopte = applicationContext.getBean(AopTest.class);
////        aopte.talk(100);
//        boolean b = aopte.password(" root ");
//        System.out.println(b);
        TransRecondService bean = applicationContext.getBean(TransRecondService.class);
        bean.log(1,2,100);

    }


}
